package com.example.eos.controller;

import com.example.eos.bean.Collec;
import com.example.eos.bean.ReturnInfo;
import com.example.eos.service.CollecService;
import com.example.eos.service.GlobalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class CollecController {

    private CollecService collecService;
    private GlobalService globalService;

    @Autowired
    public CollecController(CollecService collecService, GlobalService globalService){
        this.collecService = collecService;
        this.globalService = globalService;
    }

    //查询用户的收藏
    @RequestMapping(value = "/collec/{userid}",method = RequestMethod.GET)
    public ReturnInfo userCollec(@PathVariable String userid){
        return collecService.getUserCollec(userid);
    }

    //添加收藏
    @RequestMapping(value = "/collec",method = RequestMethod.POST)
    public ReturnInfo AddCollec(@RequestBody @Validated Collec collec, BindingResult result){
        List<String> stringList = new ArrayList<>();
        if(result.hasErrors()){
            return globalService.returnErrors(result,stringList);
        }else {
            return collecService.userAddCollec(collec, stringList);
        }
    }

    //删除收藏
    @RequestMapping(value = "/collec/{collecid}", method = RequestMethod.DELETE)
    public ReturnInfo DeleteCollec(@PathVariable String collecid){
        return collecService.userDeleteCollec(collecid);
    }

}
